package net.fltx.Ax;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JCheckBox;

public class AxUI {

	private JFrame frame;
	private JTextField textField;
	
	private AxCoer axCoer;
	private int mx,my,mk;
	private int start=0;
	private JLabel label_1;
	private JPanel panelMini;
	private JRadioButton rdbtnBl;
	private JRadioButton rdbtnA;
	private JButton btnClear;
	private JButton btnClearEx;
	private JButton btnRun;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AxUI window = new AxUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AxUI() {
		initialize();
		axCoer=new AxCoer();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("寻路演示");
		frame.setBounds(100, 100, 480, 384);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setToolTipText("");
		
		JPanel panelMap = new JPanel(){
			@Override
			public void paint(Graphics g) {
				g.setColor(new Color(0));
				g.drawRect(0, 0, 300, 300);
				if(axCoer!=null){
					axCoer.draw(g);
				}
				//super.paint(g);
			}
		};

		panelMap.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				label_1.setText("d:"+mk +"[" +e.getX()+","+e.getY()+"]");
				if(axCoer!=null && start==0){
					Point p= axCoer.getCellPoint(new Point(e.getX(), e.getY()));
					if(mk==MouseEvent.BUTTON1){
						AxCell cell= axCoer.getCell(p.x, p.y);
						if(cell.type==AxCell.PASSABLE){
							cell.type=AxCell.IMPASSABLE;
							panelMap.repaint();
						}
					}
					if(mk==MouseEvent.BUTTON3){
						AxCell cell= axCoer.getCell(p.x, p.y);
						if(cell.type==AxCell.IMPASSABLE){
							cell.type=AxCell.PASSABLE;
							panelMap.repaint();
						}
					}
				}
			}
		});
		panelMap.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				label_1.setText("c:"+e.getButton() +"[" +e.getX()+","+e.getY()+"]");
				if(axCoer!=null){
					Point p=axCoer.getCellPoint(new Point(e.getX(), e.getY()));
					AxCell ac=axCoer.getCell(p.x, p.y);
					//鼠标左键
					if(e.getButton()== MouseEvent.BUTTON1){
						System.out.println(ac.type);
						switch (ac.type) {
						case AxCell.PASSABLE:
							ac.type=AxCell.IMPASSABLE;
							break;
						case AxCell.IMPASSABLE :
							if(axCoer.pointStart==null){
								ac.type=AxCell.START;
								axCoer.pointStart=ac;
							}else if(axCoer.pointEnd==null){
								ac.type=AxCell.END;
								axCoer.pointEnd=ac;
							}
							break;
						case AxCell.START:
							if(axCoer.pointEnd==null){
								ac.type=AxCell.END;
								axCoer.pointEnd=ac;
								axCoer.pointStart=null;
							}
							break;
						case AxCell.END:
							ac.type=AxCell.PASSABLE;
							axCoer.pointEnd=ac;
							axCoer.pointEnd=null;
							break;
						default:
							break;
						}
						System.out.println(ac);
					}
					//鼠标右键清除
					if(e.getButton()== MouseEvent.BUTTON3){
						ac.type=AxCell.PASSABLE;
//						switch (ac.type) {
//
//						case AxCell.IMPASSABLE:
//							//
//							ac.type=AxCell.PASSABLE;
//							break;
//						case AxCell.START :
//							ac.type=AxCell.PASSABLE;
//							axCoer.pointStart=null;
//							break;
//						case AxCell.END:
//							if(axCoer.pointStart==null){
//								ac.type=AxCell.START;
//								axCoer.pointStart=ac;
//							}else{
//								ac.type=AxCell.PASSABLE;
//							}
//							axCoer.pointEnd=null;
//							break;
//						default:
//							break;
//						}
						
					}
					panelMap.repaint();
				}
			}
			@Override
			public void mousePressed(MouseEvent e) {
				label_1.setText("p:"+e.getButton() +"[" +e.getX()+","+e.getY()+"]");
				mk=e.getButton();
				super.mousePressed(e);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				label_1.setText("r:"+e.getButton() +"[" +e.getX()+","+e.getY()+"]");
				mk=0;
				super.mouseReleased(e);
			}
		});
	
		panelMap.setBackground(Color.WHITE);
		
		label_1 = new JLabel("白色通道,黑色障碍,绿色起点,红色终点");
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label_1, GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
						.addComponent(panelMap, GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panelMap, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 337, Short.MAX_VALUE))
					.addContainerGap())
		);
		
		rdbtnBl = new JRadioButton("遍历寻路");
		rdbtnBl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnA.setSelected(!rdbtnBl.isSelected());
			}
		});
		rdbtnBl.setSelected(true);
		
		rdbtnA = new JRadioButton("A*寻路");
		rdbtnA.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnBl.setSelected(!rdbtnA.isSelected());
			}
		});
		btnRun = new JButton("演示寻路");
		btnRun.addActionListener(new ActionListener() {
			Thread tRun=null;
			Runnable run=new  Runnable() {
				@Override
				public void run() {
					int ret=0;
					long sleep=Long.parseLong(textField.getText().trim());
					//从起点开始寻找终点
					if(rdbtnA.isSelected()) ret=axCoer.seekOnAx();
					else ret=axCoer.seekOnErgodic();
					label_1.setText("MSG:"+"[正在寻找目标...]");
					while(ret==0){
						try {
							panelMap.repaint();
							Thread.sleep(sleep);
							if(rdbtnA.isSelected()) ret=axCoer.seekOnAx();
							else ret=axCoer.seekOnErgodic();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					//从终点开始到起点生成最短线路
					if(ret==1){
						label_1.setText("MSG:"+"[找到目标,正在描绘路线...]");
					}
					while(ret==1 && !axCoer.makeLine()){
						try {
							panelMap.repaint();
							Thread.sleep(sleep);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					panelMap.repaint();
					if(ret==1){
						label_1.setText("MSG:"+"[找到目标,路线描绘完成!]");
					}
					if(ret==-1){
						label_1.setText("MSG:"+"[寻找目标失败!]");
					}
					stop();
					tRun=null;
				}
			};
			
			public void actionPerformed(ActionEvent e) {
				if(btnRun.getText().equals("演示寻路")){
					try {
						if(axCoer==null){
							label_1.setText("Error:"+"[地图初始化异常!]");
							return;
						}
						boolean isok=axCoer.pointStart!=null && axCoer.pointEnd!=null;
						if(!isok){
							label_1.setText("Error:"+"[请设置好起点和终点!]");
							return;
						}
						if(rdbtnA.isSelected()==rdbtnBl.isSelected()){
							label_1.setText("Error:"+"[请设置好寻路模式!]");
						}
						if(axCoer.point!=null) axCoer.clearEx();
						rdbtnA.setEnabled(false);
						rdbtnBl.setEnabled(false);
						btnClear.setEnabled(false);
						btnClearEx.setEnabled(false);
						btnRun.setText("停止");
						axCoer.pointStart.step=0;
						tRun=new Thread(run);
						tRun.start();
					} catch (NumberFormatException e1) {
						label_1.setText("Error:"+"[延时输入有误!]");
						e1.printStackTrace();
				}
			}else{
				stop();
				btnRun.setText("停止");
				tRun.stop();
				label_1.setText("MSG:"+"[寻路过程被停止!]");
			}
			}
		});
		
		JLabel label = new JLabel("延时");
		
		textField = new JTextField();
		textField.setToolTipText("毫秒");
		textField.setText("200");
		textField.setColumns(10);
		
		panelMini = new JPanel(){
			String sin="↖↑↗← →↙↓↘";
			@Override
			public void paint(Graphics g) {
				for(int i=0;i<3;i++){
					for(int j=0;j<3;j++){
						g.setColor(new Color(0xffffff));
						g.fillRect(j*20, i*20, 20, 20);
						g.setColor(new Color(0xffff00));
						String s=sin.substring(i*3+j, i*3+j+1);
						if(axCoer!=null && axCoer.isMod() && !" ".equals(s)){
							g.fillRect(j*20, i*20, 20, 20);
							g.setColor(new Color(0));
							g.drawString(s, j*20+5, i*20+15);
						}else if((i*3+j)%2==1){
							g.fillRect(j*20, i*20, 20, 20);
							g.setColor(new Color(0));
							g.drawString(s, j*20+5, i*20+15);
						}
						g.setColor(new Color(0xff, 0, 0));
						g.draw3DRect(j*20, i*20, 20, 20, true);
					}
				}
				//super.paint(g);
			}
		};
		panelMini.setBackground(Color.WHITE);
		panelMini.setSize(80, 80);
		JCheckBox checkBox = new JCheckBox("八方寻路");
		checkBox.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				axCoer.setMod(checkBox.isSelected());
				panelMini.repaint();
			}
		});
		
		btnClear = new JButton("清除全部");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(axCoer!=null ) axCoer.clear();
				panelMap.repaint();
			}
		});
		
		btnClearEx = new JButton("清除路迹");
		btnClearEx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(axCoer!=null && axCoer.point!=null) axCoer.clearEx();
				panelMap.repaint();
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(checkBox)
							.addContainerGap())
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(panelMini, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
							.addGap(19))))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnClear)
					.addContainerGap(26, Short.MAX_VALUE))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(rdbtnA, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
						.addComponent(rdbtnBl, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
						.addComponent(btnClearEx))
					.addContainerGap())
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(19, Short.MAX_VALUE))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnRun)
					.addContainerGap(26, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelMini, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(checkBox)
					.addGap(16)
					.addComponent(btnClear)
					.addGap(2)
					.addComponent(btnClearEx)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(rdbtnBl)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(rdbtnA)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(label)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnRun)
					.addGap(58))
		);
		panel.setLayout(gl_panel);
		frame.getContentPane().setLayout(groupLayout);
	}
	
	private void stop(){
		rdbtnA.setEnabled(true);
		rdbtnBl.setEnabled(true);
		btnClear.setEnabled(true);
		btnClearEx.setEnabled(true);
		btnRun.setText("演示寻路");
	}
	
}
